#include "fundbusidata.h"
#include <fstream>
#include <filesystem>
#include <iostream>
#include <vector>

using namespace std;
namespace fs = std::filesystem;

#define STARTLINE07 97
#define STARTLINEC2 27
#define TALINENUMBER 2

// 需要进行特殊处理的TA代码
vector<string> VspecialTaCodes = {
    "BA",
    "GD",
    "QL",
    "S8",
    "SH",
    "SM"};

void initfundBusiData07(const std::string & file,std::map<std::string,std::string> & Mfunds)
{
    ifstream file07;
    file07.open(file, ios::in);
    if (file07.fail())
    {
        return;
    }

    int startline = 0;

    while (!file07.eof())
    {

        string lineStr;
        getline(file07, lineStr);
        if (lineStr.length() == 0)
        {
            continue;
        }
        if (startline < STARTLINE07)
        {
            startline++;
            continue;
        }
        else
        {
            if (lineStr.substr(0, 8) != string("OFDCFEND"))
            {                            
                string temp_fund_name = lineStr.substr(0, 200);                
                string temp_fund_code = lineStr.substr(216, 6);
                Mfunds.insert(pair(temp_fund_code,temp_fund_name));
            }
        }
    }

    file07.close();
}

void initfundbusidataC2(const std::string & file,std::map<std::string,C2FundInfo> & Mfunds)
{
        ifstream fileC2;
    fileC2.open(file, ios::in);
    if (fileC2.fail())
    {
        return;
    }

    int startline = 0;
    string tacode;
    while (!fileC2.eof())
    {

        string lineStr;
        getline(fileC2, lineStr);
        if (lineStr.length() == 0)
        {
            continue;
        }
        // 获取TA代码
        if (startline == TALINENUMBER)
        {
            tacode = lineStr.substr(0,2);            
        }

        //对基金TA代码中带KJ的进行特殊处理（参照新意）
        for (auto &item : VspecialTaCodes)
        {
            if (tacode == item)
                tacode = "KJ" + tacode;
        }
        
        //获取基金交收参数
        if (startline < STARTLINEC2)
        { 
            startline++;
            continue;
        }
        else
        {
            if (lineStr.substr(0, 8) != string("OFDCFEND"))
            {
                int tempsg = atoi(lineStr.substr(46, 14).c_str());
                int tmepsh = atoi(lineStr.substr(62, 14).c_str());
                int tempfh = atoi(lineStr.substr(110, 14).c_str());
                string temp_fund_code = lineStr.substr(20, 6);
                C2FundInfo tempC2;
                tempC2.ta_code = tacode;

                //如果分红日期为0，则填写0，否则减1
                if(tempfh==0)
                {
                    tempC2.fh = tempfh;
                }else
                {
                    tempC2.fh = tempfh -1;
                }                
                tempC2.sh =  tmepsh - tempsg + 1;
                Mfunds.insert(pair(temp_fund_code,tempC2));
            }
        }
    }

    fileC2.close();
}

int copyfundBusiDatafile(const std::string &frompath,const std::string &topath)
{
    if (!fs::exists(fs::path(frompath)))
    {        
        return -1;
    }

    if (fs::exists(fs::path(topath)))
    {
        fs::remove_all(fs::path(topath));
    }

    fs::create_directory(fs::path(topath));

    for (auto &item : fs::recursive_directory_iterator(frompath))
    {
        //去掉不是以OFD开头的文件
        string prefix = item.path().filename().string().substr(0, 3);
        if (prefix != string("OFD"))            
            continue;

        //只拷贝07和C2文件
        string suffix = item.path().string();
        suffix = suffix.substr(suffix.length() - 6);
        if (suffix == "C2.TXT" || suffix == "C2.txt" || suffix == "07.txt" || suffix == "07.TXT")
        {
            string filename = item.path().filename().string();
            string newPath = topath + filename;
            fs::copy_file(item.path(), newPath, fs::copy_options::overwrite_existing);
        }
    }

    return 0;
}

void getC2fileList(const std::string &path, std::list<std::string> &files)
{
    for (auto &item : fs::recursive_directory_iterator(path))
    {

        string prefix = item.path().filename().string().substr(0, 3);
        if (prefix != string("OFD"))
            continue;

        string suffix = item.path().string();
        suffix = suffix.substr(suffix.length() - 6);
        if (suffix == "C2.TXT" || suffix == "C2.txt")
        {
            string filename = item.path().string();
            files.push_back(filename);
        }
    }
}

void get07fileList(const std::string &path, std::list<std::string> &files)
{
    for (auto &item : fs::recursive_directory_iterator(path))
    {
        string prefix = item.path().filename().string().substr(0, 3);
        if (prefix != string("OFD"))
            continue;

        string suffix = item.path().string();
        suffix = suffix.substr(suffix.length() - 6);
        if (suffix == "07.txt" || suffix == "07.TXT")
        {
            string filename = item.path().string();
            files.push_back(filename);
        }
    }
}